<?php
	class UserAction extends commonAction {
	public function index(){
		if ($this->checkuser())
			$msg = $_SESSION[C('USER_AUTH_KEY')]."欢迎您！";
		else
			$msg = "欢迎您来到来到WASOWA销售人员评测学习系统！请您登录或注册！";
		$this->assign('msg',$msg);
		$this->display();
	}
    public function reg(){
		$createdate = time();
		$this->assign('createdate',$createdate);
        $this->display();
    }
	
	public function adduser(){
		
		$Usermodel = D("User");
		if($Usermodel->create()) {//die($Usermodel->getError());
			$topicid = $Usermodel->add($data); //add方法会返回新添加的记录的主键值
			if($topicid) 
				$this->redirect('login','',3,'注册成功，请登录！');
			else 
				$this->error("注册失败，请联系管理员！");
				}
		else {
			echo "<script>alert('".$Usermodel->getError()."');</script>";
			echo "<script>history.go(-1);</script>"	;
			}
	}
	
	public function checkuser(){
		if (!empty($_SESSION[C('USER_AUTH_KEY')]))
			return true;
			else
			return false;
		}
		
	public function login(){
		if (!empty($_SESSION[C('USER_AUTH_KEY')])) {
			$this->redirect('index','',3,'登录成功');
			exit();
			}
		$username = $_POST['username'];
		$password = $_POST['password'];
		if (empty($username)||empty($username)) {
			$this->assign('session',$_SESSION[C('USER_AUTH_KEY')]);
			$this->display();
			}
		else 
		{
			$Usermodel = M("user");
			$vo=$Usermodel->where("username='$username' AND password='$password'")->find();
			if ($vo) {
						$_SESSION[C('USER_AUTH_KEY')]=$username;
						$this->redirect('Index/index','',3,'登录成功');
					}
			else 
				echo "登录失败！";
		}
	}
	
	public function logout(){
		session_destroy();
		$this->redirect('Index/index','',3,'登出成功');
	}
	
}
?>